package com.jspx.txweb.result;

import com.jspx.boot.environment.Environment;
import com.jspx.scriptmark.config.TemplateConfigurable;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.StringUtil;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

/**
 * 专门显示错误信息
 */
public class ErrorResult extends ResultSupport {
    private static TemplateConfigurable configurable = new TemplateConfigurable();
    static
    {
        configurable.addAutoIncludes(envTemplate.getString(Environment.autoIncludes));
    }

    public ErrorResult() {

    }

    public void execute(ActionInvocation actionInvocation) throws Exception {
        ActionSupport action = actionInvocation.getActionProxy().getAction();
        HttpServletResponse response = action.getResponse();

        //浏览器缓存控制begin
        String browserCache = action.getEnv(ActionSupport.BrowserCache);
        boolean noCache = !StringUtil.isNULL(browserCache) && !StringUtil.toBoolean(browserCache);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache,must-revalidate");
            response.setDateHeader("Expires",0);
        }
        //浏览器缓存控制end
        Map<String, String> fieldInfo = action.getFieldInfo();
        Set<String> errors = fieldInfo.keySet();
        StringBuilder sb = new StringBuilder();
        sb.append("<ul>\r\n");
        for (String msg:errors)
        {
            sb.append("<li>");
            if (Environment.warningInfo.equals(msg))
                sb.append("警告:");
            if (Environment.infoType.equals(msg))
                sb.append("信息:");
            if (Environment.message.equals(msg))
                sb.append("消息:");
            sb.append(fieldInfo.get(msg));
            sb.append("</li>");
        }
        sb.append("</ul>");
        TXWebUtil.errorPrint(sb.toString(),response,403);
    }
}